<?php

/**
 * 
 * @author NutMotion <simonotm.linux@gmail.com> 
 */
function dbGetBookmarksIdsOfTagsIds($arTagsIds) {
//note: This operation requires a loop somewhere. We had 2 choices for which layer should do the looping:
// * either inside the controller ajax/bookmarks.php. That would have required calling a dbGetBookmarksIdsOfTagsIds($tagsIds)
// for each id-tags.
// * or inside the model layer, and letting the database do the looping itself.
// We've chosen the second option. It's certainly faster, since we make only one query, and let the db search for all bookmarks
// associated with all id-tags present in an array.
// HOWEVER, this represents a choice for the global architecture. ie, is it the work of the controller to glue together
// db functions that fetch only individual items, OR is it the job of the db to collect those and returns the result to the controller?
    $queryFields = "DISTINCT webpage_id";
    $queryTable = "webpage_has_tags";

    //we build the WHERE clause
    $queryConditions = "";
    
    foreach ($arTagsIds as $i => $idTags) {
        if (strlen($queryConditions) > 0) {
            $queryConditions.=" OR ";
        }
        $queryConditions.="tags_id=" . $idTags;
    }

    return selectFromTable($queryTable, $queryFields, $queryConditions); 
//    return selectFromTable($queryTable, $queryFields, $queryConditions); 
//    This function returns the db rows in a "raw format" (that is, the output of function mysql_fect_array
//    We need to convert it before returning it
    
}

function dbGetBookmarksOfTagsIds($arTagsIds) {
    //1) we get all the bookmarks ids which have this tag
    $bookmarksIdsOfTagsIds = dbGetBookmarksIdsOfTagsIds($arTagsIds);
    
    //2) now we fetch the bookmarks entities from these ids
    $bookmarks=array();
    
    foreach ($bookmarksIdsOfTagsIds as $i => $bookmarkId) {

        $queryFields = "*";
        $queryTable = "webpage";

        //we build the WHERE clause
        $queryConditions = "id=".$bookmarkId['webpage_id']; 
        ////WARNING: we've had to modify this line several times, following our
        //modification of the format of the array returned by function selectFromTable (stored in variable $bookmarksIdsOfTagsIds here)
        //Makes me wonder wheter the usage of this function result may be too dependent ("coupled") of its format. 
        $rowBookmark=selectFromTable($queryTable, $queryFields, $queryConditions);
        $bookmarks[]=$rowBookmark[0];
    }
//        echo "////////////////////////////////////";
//    print_r($bookmarks);
//    echo "////////////////////////////////////";
    
    return $bookmarks;
}

function dbCreateWebpageHasTags($arWebpageHasTagsData) {
//   Here's how the final query we want to build here looks like:
//  INSERT INTO webpage_has_tags (webpage_id, tags_id) VALUES (6, 14)
    $queryTable = "webpage_has_tags";
    $queryFields = "webpage_id,tags_id";
    $queryValues="'".mysql_real_escape_string($arWebpageHasTagsData['webpage_id'])
    ."','".mysql_real_escape_string($arWebpageHasTagsData['tags_id'])."'";

    $resultContext=insertIntoTable2($queryTable, $queryFields, $queryValues);
    
    return $resultContext;  // for the moment, we don't do any error management in this function.
    
}

function dbGetBookmarks($arFields) {
    
}
?>
